<?php
/**
 * Created by PhpStorm.
 * User: pc
 * Date: 2025/2/20
 * Time: 17:03
 */

namespace app\modules\setting\controllers\office;


use app\common\controllers\AdminOptionsController;
use app\common\enum\EtcOfficeProductSelected_isSelectEnum;
use app\common\enum\EtcOfficeProductSelected_SendStatusEnum;
use app\common\helper\CommonHelper;
use app\models\EtcOfficeProductSelected;
use app\models\EtcSellerTiktokProduct;
use app\modules\setting\configs\office\ProductConfig;
use app\modules\setting\models\office\EtcOfficeAddress;
use app\modules\setting\models\seller\EtcSellerTiktokProductSearch;
use sookie\auth\models\Adminuser;
use yii\db\Exception;
use yii\helpers\ArrayHelper;
use Yii;

class ProductController extends AdminOptionsController
{
    /**
     * @return array
     * 列表数据
     */
    public function actionIndex()
    {
        try {
            $searchModel = new EtcSellerTiktokProductSearch();
            $dataProvider = $searchModel->searchOffice(Yii::$app->request->queryParams);

            $data = $dataProvider->getModels();
            if($data){
                $data = array_map(function($item){
                    $re = $item;
                    if(is_object($item)){
                        $re = $item->toArray();
                    }
                    $arr_pic = \yii\helpers\Json::decode($item['img'],true);
                    $pic = ArrayHelper::getValue($arr_pic,'0');
                    $pic = CommonHelper::getMarketPlacePic($pic);

                    $arn = CommonHelper::float($item['price'] * $item['seller_commission_percent'] /100);
                    $seller_commission_percent = CommonHelper::float($item['seller_commission_percent']);

                    $re['img'] = $pic;
                    $re['arn'] = $arn;
                    $re['seller_commission_percent'] = $seller_commission_percent;
                    return $re;
                },$data);
            }

            return ArrayHelper::merge(["data" =>$data, "pagination" => $dataProvider->getPagination()],ProductConfig::configIndex());
        }catch (\Throwable $e){
            return ["ok" => false,"msg" => $e->getMessage(),"code" => 401];
        }
    }

    /**
     * @param $id
     * @return array
     * 修改接口
     */
    public function actionUpdate($id)
    {
        $tran = Yii::$app->db->beginTransaction();
        try {

            CommonHelper::notOfficeThrow();

            $model = $this->findModel($id);

            $model_selected = EtcOfficeProductSelected::find()->andWhere(['product_id'=>$id])->one();

            if (Yii::$app->request->isPost){
                $model->is_select = 1;
                $model->save();
                $send_sample_num = floatval(Yii::$app->request->post("send_sample_num"));
                if($send_sample_num <= 0){
                    throw new Exception("请填写申请数量");
                }
                $send_sample_address_id = floatval(Yii::$app->request->post("send_sample_address_id"));
                $address = EtcOfficeAddress::find()->andWhere(['id'=>$send_sample_address_id])->one();
                if(empty($address)){
                    throw new Exception("地址不存在");
                }

                if(empty($model_selected)){
                    $info = Adminuser::getUserInfo();

                    $model_selected = new EtcOfficeProductSelected();
                    $model_selected->product_id = $id;
                    $model_selected->seller_id = $model->user_id;
                    $model_selected->user_id = $info->id;
                    $model_selected->is_select = EtcOfficeProductSelected_isSelectEnum::YES;
                    $model_selected->goods_id = $model->goods_id;
                    $model_selected->apply_time = date("Y-m-d H:i:s");
                    $model_selected->send_status = EtcOfficeProductSelected_SendStatusEnum::NO;
                    $model_selected->code = $model->product_id . "_".$info->id."_".date("md");
                }
                $model_selected->send_sample_num = $send_sample_num;
                $model_selected->send_sample_address_id = $address->id;

                if ($model_selected->save()) {
                    $tran->commit();
                    return ["msg" => "修改成功"];
                }else{
                    return ["ok" => false,"msg" => CommonHelper::getFirstErrorStr($model_selected->errors),"code" => 401];
                }
            }
            return ProductConfig::configUpdate($id,$model_selected);
        }catch (\Throwable $e){
            $tran->rollBack();
            return ["ok" => false,"msg" => $e->getMessage(),"code" => 401];
        }
    }

    protected function findModel($id)
    {
        if (($model = EtcSellerTiktokProduct::findOne($id)) !== null) {
            return $model;
        }

        throw new Exception('不存在的记录');
    }
}